package com.nttuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nttuan.model.dao.ContactDao;
import com.nttuan.model.dao.service.ContactService;
import com.nttuan.model.entity.Contact;
import com.nttuan.model.entity.Task;
import com.nttuan.model.entity.Track;
import com.nttuan.mycalender.R;
import com.nttuan.util.LogSupport;
import com.nttuan.util.Support;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by TuanNT on 11/27/2014.
 */
public class NotificationAdapter extends BaseAdapter {
    public static final String TAG = "NotificationAdapter";
    public static final String NOTHING = "nothing";
    private Context context;
    private LayoutInflater inflater;
    private List<Track> list;
    private ViewHoller holer;
    private Date now;
    private ContactService contactService;
    private Date savePoint;

    public NotificationAdapter(Context context, List<Track> list) {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        this.now = Calendar.getInstance().getTime();
        this.contactService = ContactDao.getInstance(context);
        this.savePoint = LogSupport.lastTimeNotifi(context);
    }

    public void setSavePoint(Date savePoint) {
        this.savePoint = savePoint;
    }

    public void setList(List<Track> list) {
        if (list == null)
            this.list = new ArrayList<Track>();
        else
            this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            holer = new ViewHoller();
            view = inflater.inflate(R.layout.item_notification, null);
            holer.lastTime = (TextView) view.findViewById(R.id.textLastItmeNotifiItem);
            holer.title = (TextView) view.findViewById(R.id.textTitleNotifiItem);
            holer.message = (TextView) view.findViewById(R.id.textMessageNotifiItem);
            holer.icon = (ImageView) view.findViewById(R.id.imgNotifiItem);
 //           holer.layoutAction = (LinearLayout) view.findViewById(R.id.lLayoutActionNotifiItem);
            holer.layoutBg = (LinearLayout) view.findViewById(R.id.lLayoutBgNotfiItem);
            view.setTag(holer);
        } else
            holer = (ViewHoller) view.getTag();
        Track track = list.get(i);

        defaultStyle(true);
        switch (track.getActon()) {
            case Track.ACTION_ALARM:
                alarm(track, now);
                break;
            case Track.ACTION_EVENT_START:
                eventStart(track, now);
                break;
            case Track.ACTION_RECEIVE_INVITE:
                invite(track);
                break;
            case Track.ACTION_RECEIVE_ACCEPT:
                acceptInvite(track);
                break;
            case Track.ACTION_RECEIVE_UPDATE_INVITE:
                updateInvite(track);
                break;
            case Track.ACTION_RECEIVE_DELETE_INVITE:
                deleteInvite(track);
                break;
        }
        if (Support.equalDate(now, track.getLastTime()))
            holer.lastTime.setText(Support.displayTime(track.getLastTime()));
        else {
            String time = Support.displayTime(track.getLastTime());
            String date = Support.displayDate(track.getLastTime());
            holer.lastTime.setText(context.getString(R.string.last_time_notifi, time, date));
        }
        if (savePoint.getTime() - track.getLastTime().getTime() >= 0)
            holer.layoutBg.setBackgroundResource(R.drawable.coming_second_bg);
        else
            holer.layoutBg.setBackgroundResource(R.drawable.coming_primary_bg);
        return view;
    }
    private void deleteInvite(Track track){
//        view
        holer.icon.setVisibility(View.GONE);
        defaultStyle(false);
//        data
        holer.title.setText(context.getString(R.string.title_notifi_canceled, track.getTask().getTaskName()));
        String message = track.getMessage();
        if(message !=null && !message.equals(""))
            holer.message.setText(message);
        else
            holer.message.setText(R.string.message_notifi_canceled);
    }
    private void updateInvite(Track track){
        //        view
        holer.icon.setVisibility(View.VISIBLE);
        holer.icon.setImageResource(R.drawable.ic_sync_white_24dp);
//        holer.layoutAction.setVisibility(View.VISIBLE);
        defaultStyle(true);
//        data
        holer.title.setText(track.getTask().getTaskName());
        String message = track.getMessage();
        if(message !=null && !message.equals(""))
            holer.message.setText(message);
        else
            holer.message.setText(R.string.message_update_invite);
    }
    private void acceptInvite(Track track) {
//        view
        holer.icon.setVisibility(View.GONE);
//        holer.layoutAction.setVisibility(View.VISIBLE);
        defaultStyle(false);
//        data
        holer.title.setText(track.getTask().getTaskName());
        holer.message.setText(track.getMessage());
    }
    private void alarm(Track track, Date now) {
//        view
        Task task = track.getTask();
//        holer.layoutAction.setVisibility(View.GONE);
        holer.icon.setVisibility(View.VISIBLE);
//        data

        String message;
        if (task.getRemind() < 1440) {
            Date start = Support.sysDate(now, task.getStart());
            message = context.getString(R.string.title_alarm_item_now, Support.displayTime(start));
        } else {
            String time = Support.displayTime(task.getStart());
            String date = Support.displayDate(task.getStart());
            int iDateOfWeek = Support.dateOfWeek(task.getStart());
            message = context.getString(R.string.title_alarm_item, time, date, iDateOfWeek + "");
        }
        holer.message.setText(message);
        holer.title.setText(task.getTaskName());
        holer.icon.setImageResource(R.drawable.ic_access_alarm_white_24dp);
    }

    private void eventStart(Track track, Date now) {
//        view
        Task task = track.getTask();
//        holer.layoutAction.setVisibility(View.GONE);
        holer.icon.setVisibility(View.VISIBLE);
//        data
        holer.title.setText(task.getTaskName());
        holer.message.setText(context.getText(R.string.title_notifi_started));
        holer.icon.setImageResource(R.drawable.ic_play_arrow_white_24dp);
    }

    private void defaultStyle(boolean isDefault) {
        if (isDefault) {
            holer.title.setTextSize(22f);
            holer.message.setTextSize(16f);
            return;
        }
        holer.title.setTextSize(16f);
        holer.message.setTextSize(12f);
    }

    private void invite(Track track) {
//        view
        holer.icon.setVisibility(View.GONE);
//        holer.layoutAction.setVisibility(View.VISIBLE);
        defaultStyle(false);
//        data
        holer.title.setText(R.string.title_invite_defaule);
        List<Contact> listContacts = contactService.contactsByTask(track.getTask().getTaskId());
        for (Contact contact : listContacts)
            if (contact.isLeader()) {
                holer.title.setText(context.getString(R.string.title_invite, contact.getUser().getUserName()));
                break;
            }
        if(track.getMessage() != null && !track.getMessage().equals(""))
           holer.message.setText(track.getMessage());
        else
            holer.message.setText(R.string.message_invite_default);
    }

    class ViewHoller {
        TextView lastTime;
        TextView title;
        TextView message;
        ImageView icon;
//       LinearLayout layoutAction;
        LinearLayout layoutBg;

    }
}
